﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MachineVision.Extensions;
using MachineVision.Models;
using MachineVision.Shared.Events;
using Prism.Events;
using Prism.Mvvm;

namespace MachineVision.Services
{
    public class NavigationService : BindableBase, INavigationService
    {
        public NavigationService(IEventAggregator aggregator)
        {
            MenuItems = new ObservableCollection<NavigationItem>();
            this.aggregator = aggregator;
            this.aggregator.GetEvent<LanguageEventBus>().Subscribe(OnLanguageChanged);
        }

        private void OnLanguageChanged(bool obj)
        {
            Reflush();
        }

        void Reflush()
        {
            foreach (var item in MenuItems)
            {
                var val = LanguageHelper.KeyValues[item.Key];
                if (val != null)
                {
                    item.Name = val;
                    if (item.Items != null)
                    {
                        foreach (var subitem in item.Items)
                        {
                            var val2 = LanguageHelper.KeyValues[subitem.Key];
                            if (val2 != null)
                            {
                                subitem.Name = val2;
                                if (subitem.Items != null)
                                {
                                    foreach (var subitem2 in subitem.Items)
                                    {
                                        var val3 = LanguageHelper.KeyValues[subitem2.Key];
                                        if (val3 != null)
                                        {
                                            subitem2.Name = val3;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        public void InitMenus()
        {
            MenuItems.Clear();
            MenuItems.Add(
                new NavigationItem(
                    "All",
                    "全部",
                    "",
                    "",
                    new ObservableCollection<NavigationItem>()
                    {
                        new NavigationItem(
                            "TemplateMatch",
                            "模板匹配",
                            "",
                            "",
                            new ObservableCollection<NavigationItem>()
                            {
                                //new NavigationItem("OutlineMatch","轮廓匹配","ShapeCirclePlus","DrawShapeView"),
                                new NavigationItem(
                                    "ShapeMatch",
                                    "形状匹配",
                                    "ShapeOutline",
                                    "ShapeView"
                                ),
                                new NavigationItem("NccMatch", "相似性匹配", "Clouds", "NccView"),
                                new NavigationItem(
                                    "DeformationMatch",
                                    "形变匹配",
                                    "ShapeOutline",
                                    "LocalDeformableView"
                                ),
                            }
                        ),
                        new NavigationItem(
                            "Measure",
                            "比较测量",
                            "",
                            "",
                            new ObservableCollection<NavigationItem>()
                            {
                                new NavigationItem(
                                    "Caliper",
                                    "卡尺找圆",
                                    "Circle",
                                    "CircleMeasureView"
                                ),
                                //new NavigationItem("Color","颜色检测","Palette",""),
                                //new NavigationItem("GeometricMeasurement","几何测量","Ruler","")
                            }
                        ),
                        new NavigationItem(
                            "Character",
                            "字符识别",
                            "",
                            "",
                            new ObservableCollection<NavigationItem>()
                            {
                                //new NavigationItem("Character","字符识别","FormatColorText",""),
                                new NavigationItem("BarCode", "一维码识别", "BarCode", "BarCodeView"),
                                new NavigationItem("QrCode", "二维码识别", "QrCode", "QrCodeView"),
                            }
                        ),
                        new NavigationItem(
                            "Defect",
                            "缺陷检测",
                            "",
                            "",
                            new ObservableCollection<NavigationItem>()
                            {
                                new NavigationItem("Difference", "缺陷检测", "Crop", "DefectView"),
                                //new NavigationItem("Deformable","形变模型","CropRotate",""),
                            }
                        )
                    }
                )
            );
            //MenuItems.Add(new NavigationItem("TemplateMatch", "模板匹配", "", ""));
            //MenuItems.Add(new NavigationItem("Measure", "比较测量", "", ""));
            ////MenuItems.Add(new NavigationItem("Character", "字符识别", "", ""));
            //MenuItems.Add(new NavigationItem("Defect", "缺陷检测", "", ""));
            MenuItems.Add(new NavigationItem("Document", "学习文档", "Null", ""));
            MenuItems.Add(new NavigationItem("Setting", "系统设置", "Null", "SettingView"));
        }

        private ObservableCollection<NavigationItem> _menuitems;
        private readonly IEventAggregator aggregator;

        public ObservableCollection<NavigationItem> MenuItems
        {
            get { return _menuitems; }
            set { SetProperty(ref _menuitems, value); }
        }
    }
}
